/* Copyright (c) 2006 by M Aamir Maniar
 * 
 * Permission is hereby granted, free of charge, to any person obtaining 
 * a copy of this software and associated documentation files (the 
 * "Software"), to deal in the Software without restriction, including 
 * without limitation the rights to use, copy, modify, merge, publish, 
 * distribute, sublicense, and/or sell copies of the Software, and to 
 * permit persons to whom the Software is furnished to do so, subject to 
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be 
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 * 
 * */

namespace Generic.Algorithms.Sorting
{
    #region Using Declarations
    using System;
    using System.Collections.Generic;
    using System.Collections;
    using System.Text;
    #endregion

    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <remarks>
    /// <see cref="ListSorter&lt;T&gt;"/> helps sorting of non generic <see cref="IList"/> based objects using 
    /// generic sorting stratagies. Before sorting takes place each time an <see cref="IList"/> based object is 
    /// converted into <see cref="IList&lt;T&gt;"/> based objects. 
    /// </remarks>
    /// <example>
    /// public class ListSortTester
    /// {
    ///     public ListSortTester()
    ///     {
    ///     
    ///     }
    /// }
    /// </example>
    public class ListSorter<T> : ISorter<T>
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ListSorter&lt;T&gt;"/> class.
        /// </summary>
        /// <param name="list">The list.</param>        
        public ListSorter(IList list)
        {
            _list = list;
        }

        #region public ListSorter(IList list, ISortStrategy<T> strategy)
        /// <summary>
        /// Initializes a new instance of the <see cref="ListSorter&lt;T&gt;"/> class.
        /// </summary>
        /// <param name="list">The list.</param>
        /// <param name="strategy">The strategy.</param>
		public ListSorter(IList list, ISortStrategy<T> strategy)
		{
            _strategy = strategy;
            _list = list;
        }
        #endregion

        #region public void Sort()
        /// <summary>
        /// Sorts non generic list and return IList<typeparamref name="T"/>.
        /// </summary>
        /// <returns>
        /// Returns sorted IList<typeparamref name="T"/>.
        /// </returns>
        public IList<T> Sort()
        {
            return Sort(SortDirection.Ascending);
        }
        #endregion

        #region public IList<T> Sort(SortDirection direction)
        /// <summary>
        /// Sorts non generic list and return IList<typeparamref name="T"/>.
        /// </summary>
        /// <param name="direction">The direction.</param>
        /// <returns>
        /// Returns sorted IList<typeparamref name="T"/>.
        /// </returns>
        public IList<T> Sort(SortDirection direction)
        {
            _strategy.List = ToGenericList(_list);
            _strategy.Sort(direction);
            return _strategy.List;
        }
        #endregion

        #region public IList<T> ToGenericList(IList list)
        /// <summary>
        /// Converts IList based object to IList&lt;T&gt; based object.
        /// </summary>
        /// <param name="list">The list.</param>
        /// <returns></returns>
        public IList<T> ToGenericList(IList list)
        {
            List<T> newList = new List<T>();
            for (int index = 0; index < list.Count; index++)
            {
                newList.Add((T)list[index]);
            }
            return newList;
        }
        #endregion

        #region public ISortStrategy<T> Strategy
        ISortStrategy<T> _strategy;
        /// <summary>
        /// Gets or sets the strategy.
        /// </summary>
        /// <value>The strategy.</value>
        public ISortStrategy<T> Strategy
        {
            get
            {
                return _strategy;
            }
            set
            {
                _strategy = value;
            }
        }
        #endregion

        #region public IList List
        private IList _list;
        /// <summary>
        /// Gets or sets the list.
        /// </summary>
        /// <value>The list.</value>
        public IList List
        {
            get { return _list; }
            set { _list = value; }
        }
        #endregion

        #region public IComparer<T> Comparer
        private IComparer<T> _comparer;
        /// <summary>
        /// Gets or sets the comparer.
        /// </summary>
        /// <value>The comparer.</value>
        public IComparer<T> Comparer
        {
            get { return _comparer; }
            set { _comparer = value; }
        }
        #endregion

    }
}
